<?php
/**
 * @author hequan
 * @date	2012-10-31
 * @todo	更新商品标准属性的一些关键列值
 */

require dirname(dirname(dirname(dirname(__FILE__)))) . '/www/index.php';
require ROOT_PATH . '/lib/openapi/top/TopSdk.php';

error_reporting(E_ALL|E_STRICT);
ini_set("display_errors","on");
ini_set('memory_limit', -1);
ini_set('auto_detect_line_endings', 'on');
set_time_limit(0);
setlocale(LC_ALL, 'zh_CN');

// 获取渠道授权信息
$channel_id = 1;
$channelinfo_obj = Factory::getModule('channelinfo', true);
$channelinfo_data = $channelinfo_obj->getChannelInfo($channel_id);

$topclient = new TopClient();
$topclient->appkey = $channelinfo_data['appkey'];
$topclient->secretKey = $channelinfo_data['secretkey'];
$sessionkey = $channelinfo_data['sessionkey'];



$taobaocat_obj = Factory::getData('taobaocat');
$taobaoitemprop_obj = Factory::getData('taobaoitemprop');
$taobaoitempropvalue_obj = Factory::getData('taobaoitempropvalue');


$condition = array(
	'fields'	=> array('cid'),
	'where'		=> array('is_parent' => 0),
);
$itemcat_arr = $taobaocat_obj->fetchAll($condition);
foreach($itemcat_arr as $row)
{
	// 获取标准商品类目属性
	$itempropsget = new ItempropsGetRequest();
	$itempropsget->setFields('is_allow_alias,is_key_prop,is_color_prop,is_enum_prop,is_input_prop,is_item_prop,is_sale_prop,multi,must,name,parent_pid,parent_vid,pid,prop_values,sort_order,status');
	$itempropsget->setCid($row['cid']);
	$itempropsget_arr = $topclient->execute($itempropsget);
	if(!isset($itempropsget_arr['item_props']['item_prop'])){
		var_dump($itempropsget_arr);
		printf("cid = %s\n", $row['cid']);
		continue;
	}
	$itempropsget_arr = $itempropsget_arr['item_props']['item_prop'];

	foreach($itempropsget_arr as $data)
	{
		$condition = array(
			'where'	=> array('cid' => $row['cid'], 'pid' => $data['pid']),
			'data'	=> array(
				'is_input_prop'		=> $data['is_input_prop'],
				'is_enum_prop'		=> $data['is_enum_prop'],
				'is_item_prop'		=> $data['is_item_prop'],
				'must'				=> $data['must'],
				'multi'				=> $data['multi'],
			),
		);

		$ret = $taobaoitemprop_obj->update($condition);
		if($ret === false){
			printf("update t_item_prop_1 failed. sql = %s\n", $taobaoitemprop_obj->getLastSql());
			exit(1);
		}
	}
}

